// import type { User } from '@/types/user'
import { getTreeApi } from '@/api/system'
import { ElMessage } from 'element-plus'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { YingYongTree } from '@/utils/publicMethods'
/**
 * @Author: 段晓龙
 * @Date: 2024-09-10 15:0:19
 * @Description: 树结构存储
 * @LastEditors: 段晓龙
 */
export const usetreeStore = defineStore(
  'cnac-tree',
  () => {
    // 地区树结构
    const regionTreeList = ref<any>([])
    const setRegionTree = (info: any) => (regionTreeList.value = info)
    const getRegionTree = async () => {
      const params = {
        id: 0,
        sqlId: 'sys.getRegionTree'
      }
      await getTreeApi(params)
        .then((res) => {
          setRegionTree(res)
        })
        .catch(() => {
          ElMessage({ message: '树获取失败', type: 'error' })
        })
    }
    const clearRegionTree = () => (regionTreeList.value = [])
    // 划小树结构
    const huaXiaoTreeList = ref<any>([])
    const setHuaXiaoTree = (info: any) => (huaXiaoTreeList.value = info)
    const getHxTree = async () => {
      const params = {
        id: 0,
        sqlId: 'sys.getHxTree'
      }
      await getTreeApi(params)
        .then((res: any) => {
          setHuaXiaoTree(res)
        })
        .catch(() => {
          ElMessage({ message: '树获取失败', type: 'error' })
        })
    }
    const clearHuaXiaoTree = () => (huaXiaoTreeList.value = [])
    // 应用分类树结构
    const appTreeList = ref<any>([])
    const setAppTree = (info: any) => (appTreeList.value = info)
    const getSubClsTree = async () => {
      const params = {
        sqlId: 'appClassMgr.getAppClass',
        id: 0
      }
      await getTreeApi(params)
        .then((res: any) => {
          setAppTree(JSON.parse(JSON.stringify(YingYongTree(res))))
        })
        .catch(() => {
          ElMessage({ message: '分类数据获取失败', type: 'error' })
        })
    }
    const clearAppTree = () => (appTreeList.value = [])
    // 维表列名称
    const columnList = ref<any>([])
    const setColumnList = (info: any) => (columnList.value = info)
    const clearColumnList = () => (columnList.value = [])
    return {
      regionTreeList,
      setRegionTree,
      getRegionTree,
      clearRegionTree,
      huaXiaoTreeList,
      setHuaXiaoTree,
      getHxTree,
      clearHuaXiaoTree,
      appTreeList,
      setAppTree,
      getSubClsTree,
      clearAppTree,
      columnList,
      setColumnList,
      clearColumnList
    }
  },
  {
    persist: true // 开启持久化
  }
)
